﻿using LogLib;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IDCardReaderCZ.Detect
{
    public class Tools
    {
        public static bool SfzImageJudge(string sfzzm, string sfzfm)
        {
            try
            {
                if (string.IsNullOrEmpty(sfzzm) || string.IsNullOrEmpty(sfzfm))
                    return false;

                if (!File.Exists(sfzzm) || !File.Exists(sfzfm))
                    return false;

                System.IO.FileStream fs1 = new System.IO.FileStream(sfzzm, FileMode.Open, FileAccess.Read);
                Image img1 = System.Drawing.Image.FromStream(fs1);
                fs1.Close();

                System.IO.FileStream fs2 = new System.IO.FileStream(sfzfm, FileMode.Open, FileAccess.Read);
                Image img2 = System.Drawing.Image.FromStream(fs2);
                fs2.Close();

                var face = FaceDetect.FindFace(sfzzm);
                if (face.Count > 0)   //file1是人像面，file2是国徽面
                {
                    Log.WriteAsync($"在{sfzzm}上找不到人脸");
                    if (face[0].X > img1.Width / 2)  //人像在右侧，说明图像是正的，什么都不用变
                    {
                        Log.WriteAsync($"在{sfzzm}上找到人脸，人脸在右侧");
                    }
                    else  //人像在左侧，说明图像是反的，需要旋转180
                    {
                        Log.WriteAsync($"在{sfzzm}上找到人脸，人脸在左侧");

                        img1.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        img1.Save(sfzzm);

                        img2.RotateFlip(RotateFlipType.Rotate180FlipNone);
                        img2.Save(sfzfm);
                    }
                }
                else
                {
                    face = FaceDetect.FindFace(sfzfm);
                    if (face.Count > 0)   //file2是人像面，file1是国徽面
                    {
                        
                        if (face[0].X > img2.Width / 2)  //人像在右侧，说明图像是正的
                        {
                            Log.WriteAsync($"在{sfzfm}上找到人脸，人脸在右侧");
                            File.Move(sfzzm, sfzzm + "bak");
                            File.Move(sfzfm, sfzfm + "bak");
                            File.Move(sfzzm + "bak", sfzfm);
                            File.Move(sfzfm + "bak", sfzzm);
                        }
                        else  //人像在左侧，说明图像是反的，需要旋转180
                        {
                            Log.WriteAsync($"在{sfzfm}上找到人脸，人脸在左侧");

                            img1.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            img1.Save(sfzfm);

                            img2.RotateFlip(RotateFlipType.Rotate180FlipNone);
                            img2.Save(sfzzm);
                        }
                    }
                    else
                    {
                        Log.WriteAsync($"在{sfzfm}上找不到人脸，判断方向失败");
                        return false;  //都找不到人像
                    }
                }

                return true;
            }
            catch(Exception ex)
            {
                Log.WriteException("判断证件方向异常："+ex.ToString());
                return false;
            }
        }
    }
}
